//    Travel US - For users of travel.usairways.com
//    intended for US Airways employees and their dependents
//    This app will help you track your flights and display
//    Availability, flight status and other non-rev info
//    Copyright (C) 2014  Renats Stozkovs
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

package com.bc004346.travelus.model.domain;

import android.support.annotation.NonNull;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;

import static com.bc004346.travelus.utility.Utility.DAY;
import static com.bc004346.travelus.utility.Utility.MONTH_DAY_DATE;

/**
 * Created by Renats on 1/20/14.
 * <p/>
 * Flight class
 */
public class Flight implements Comparable<Flight>, Serializable {

    //<editor-fold desc="Members">

    private static final Long serialVersionUID = 2416000000002L;
    private String mOrigin;
    private String mDestination;
    private Date mDeparture;
    private Date mArrival;
    private Integer mFlightNumber;
    private Boolean mIsCheckedIn = false;
    private Boolean mIsCheckInScheduled = false;
    private Availability mFirstClass;
    private Availability mCoach;
    private String mPnr;
    private String mStatus;
    private Boolean mSelected;
    private String mFlightIndex = "";
    private int mFlightPosition;
    private Boolean mTracked;
    private Boolean mIsCheckedin;

    //</editor-fold>
    //<editor-fold desc="Constructor">

    public Flight() {
    }

    public Flight(Integer flightNumber,
                  Date departure,
                  String origin,
                  String destination,
                  Date arrival,
                  Boolean isCheckedIn,
                  String pnr,
                  Boolean isCheckInScheduled) {
        setFlightNumber(flightNumber);
        setDeparture(departure);
        setOrigin(origin);
        setDestination(destination);
        setArrival(arrival);
        setIsCheckedIn(isCheckedIn);
        setCoach(new Availability());
        setFirstClass(new Availability());
        setPnr(pnr);
        setIsCheckInScheduled(isCheckInScheduled);
        setFlightStatus("N/A");
    }

    public Flight(Integer flightNumber,
                  Date departure,
                  String origin,
                  String destination,
                  Date arrival,
                  Integer firstClass,
                  Integer coach,
                  String flightIndex,
                  Boolean isTracked) {
        setFlightNumber(flightNumber);
        setDeparture(departure);
        setOrigin(origin);
        setDestination(destination);
        setArrival(arrival);
        setFirstClass(new Availability());
        getFirstClass().setSeatsAvailable(firstClass);
        setCoach(new Availability());
        getCoach().setSeatsAvailable(coach);
        setFlightStatus("N/A");
        setFlightIndex(flightIndex);
        setTracked(isTracked);
    }

    public Flight(Integer flightNumber,
                  String origin,
                  String destination,
                  Date departureDate,
                  Integer firstCapacity,
                  Integer firstSpaceAvailable,
                  Integer firstNonRev,
                  Integer coachCapacity,
                  Integer coachSpaceAvailable,
                  Integer coachNonRev) {
        setFlightNumber(flightNumber);
        setOrigin(origin);
        setDestination(destination);
        setDeparture(departureDate);
        setFirstClass(new Availability());
        getFirstClass().setCapacity(firstCapacity);
        getFirstClass().setSeatsAvailable(firstSpaceAvailable);
        getFirstClass().setNonRevsCount(firstNonRev);
        setCoach(new Availability());
        getCoach().setCapacity(coachCapacity);
        getCoach().setSeatsAvailable(coachSpaceAvailable);
        getCoach().setNonRevsCount(coachNonRev);
        setFlightStatus("N/A");
    }

    //</editor-fold>
    //<editor-fold desc="Properties">

    public String getOrigin() {
        return mOrigin;
    }

    public void setOrigin(String origin) {
        this.mOrigin = origin;
    }

    public String getDestination() {
        return mDestination;
    }

    public void setDestination(String destination) {
        this.mDestination = destination;
    }

    public Date getDeparture() {
        return mDeparture;
    }

    public void setDeparture(Date departure) {
        this.mDeparture = departure;
    }

    public Date getArrival() {
        return mArrival;
    }

    public void setArrival(Date arrival) {
        this.mArrival = arrival;
    }

    public void setIsCheckedIn(boolean isCheckedIn) {
        this.mIsCheckedIn = isCheckedIn;
    }

    public boolean isCheckedIn() {
        return mIsCheckedIn;
    }

    public int getFlightNumber() {
        return mFlightNumber;
    }

    public void setFlightNumber(int flightNumber) {
        this.mFlightNumber = flightNumber;
    }

    public Availability getFirstClass() {
        return mFirstClass;
    }

    void setFirstClass(Availability mFirstClass) {
        this.mFirstClass = mFirstClass;
    }

    public Availability getCoach() {
        return mCoach;
    }

    void setCoach(Availability coach) {
        this.mCoach = coach;
    }

    public String getPnr() {
        return mPnr;
    }

    void setPnr(String mPnr) {
        this.mPnr = mPnr;
    }

    public boolean isCheckInScheduled() {
        return mIsCheckInScheduled;
    }

    public void setIsCheckInScheduled(boolean mIsCheckInScheduled) {
        this.mIsCheckInScheduled = mIsCheckInScheduled;
    }

    public String getFlightStatus() {
        return mStatus;
    }

    public void setFlightStatus(String mStatus) {
        this.mStatus = mStatus;
    }

    public Boolean isSelected() {
        return mSelected;
    }

    public void setSelected(Boolean mSelected) {
        this.mSelected = mSelected;
    }

    public String getFlightIndex() {
        return mFlightIndex;
    }

    public void setFlightIndex(String mFlightIndex) {
        this.mFlightIndex = mFlightIndex;
    }

    public int getFlightPosition() {
        return mFlightPosition;
    }

    public void setFlightPosition(int mFlightPosition) {
        this.mFlightPosition = mFlightPosition;
    }

    public Boolean isTracked() {
        return mTracked;
    }

    public void setTracked(Boolean mTracked) {
        this.mTracked = mTracked;
    }

    //</editor-fold>
    //<editor-fold desc="Overrides">

    @Override
    public String toString() {
        String date;
        boolean closeToDayOf = getDeparture().getTime() < new Date().getTime() + DAY * 3;
        if (closeToDayOf &&
                getDeparture().getDate() == new Date().getDate())
            date = "today";
        else if (closeToDayOf &&
                getDeparture().getDate() == new Date().getDate() + 1)
            date = "tomorrow";
        else
            date = "on " + MONTH_DAY_DATE.format(getDeparture());

        return String.format("%s%d %s-%s %s",
                getPnr() == null ? "" : getPnr() + ": ",
                getFlightNumber(),
                getOrigin(),
                getDestination(),
                date);
    }

    @Override
    public int compareTo(@NonNull Flight flight) {

        // same flight on the same date
        if (flight.getFlightNumber() == this.getFlightNumber()
                && flight.getOrigin().equals(this.getOrigin())
                && flight.getDeparture().equals(this.getDeparture())) return 0;

        // otherwise sort by departure date
        if (this.getDeparture().before(flight.getDeparture())) return -1;
        else return 1;
    }

    //</editor-fold>
    //<editor-fold desc="Serializable members">

    private void readObject(
            ObjectInputStream aInputStream
    ) throws ClassNotFoundException, IOException {
        //always perform the default de-serialization first
        aInputStream.defaultReadObject();
    }

    private void writeObject(
            ObjectOutputStream aOutputStream
    ) throws IOException {
        //perform the default serialization for all non-transient, non-static fields
        aOutputStream.defaultWriteObject();
    }


    //</editor-fold>
}
